<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="addClick">添加分类</el-button>
      <!-- 添加分类的对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="dialogVisible"
        width="50%"
        @close="closeAddUser"
      >
        <el-form
          :model="addCate"
          :rules="addCateRules"
          ref="addCateRuleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCate.cat_name"></el-input>
          </el-form-item>
          <!-- options指定数据源 -->
          <!-- props指定配置对象 -->
          <!-- 级联选择器双向绑定到数组中 -->
          <!-- props.checkStrictly = true 来设置父子节点取消选中关联，从而达到选择任意一级选项的目的 -->
          <el-form-item label="父级分类">
            <el-cascader
              v-model="selectedItem"
              :options="parentCateList"
              :props="parentKeys"
              clearable
              @change="handleChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancelCate">取 消</el-button>
          <el-button type="primary" @click="addCateInfo">确 定</el-button>
        </span>
      </el-dialog>
      <tree-table
        :data="tableData"
        :columns="columns"
        border
        :expand-type="false"
        :selection-type="false"
        show-index
        index-text="#"
      >
        <template slot="isok" slot-scope="scope"
          ><i
            v-if="scope.row.cat_deleted === true"
            style="color: green"
            class="el-icon-success"
          ></i>
          <i v-else style="color: red" class="el-icon-error"></i
        ></template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" type="success">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="warning"
            >二级</el-tag
          >
          <el-tag v-else type="danger">三级</el-tag>
        </template>
        <template slot="opt">
          <el-button type="primary" size="mini" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button type="warning" size="mini" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 20
      },
      total: 0,
      tableData: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          prop: 'cat_level',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      addCate: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      // 父级级联选择器二级数据
      parentCateList: [],
      selectedItem: [],
      parentKeys: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  created() {
    this.getTreeList()
  },
  methods: {
    handleChange() {
      console.log(this.selectedItem)
      if (this.selectedItem.length > 0) {
        this.addCate.cat_level = this.selectedItem.length
        this.addCate.cat_pid = this.selectedItem[this.selectedItem.length - 1]
      } else {
        this.addCate.cat_level = 0
        this.addCate.cat_pid = 0
      }
      console.log(this.addCate)
    },
    addClick() {
      this.getParentList()
      this.dialogVisible = true
    },
    closeAddUser() {
      this.$refs.addCateRuleForm.resetFields()
      this.selectedItem = []
      this.addCate.cat_pid = 0
      this.addCate.cat_level = 0
    },
    cancelCate() {
      this.dialogVisible = false
      this.$refs.addCateRuleForm.resetFields()
    },
    // 添加分类数据
    addCateInfo() {
      this.$refs.addCateRuleForm.validate((valid) => {
        if (valid) {
          this.$http.post('categories', this.addCate).then((res) => {
            this.dialogVisible = false
            this.getTreeList()
            this.$message.success('添加成功')
          })
        } else {
          this.$message.error('添加失败')
        }
      })
    },
    getTreeList() {
      this.$http
        .get('categories', {
          params: this.queryInfo
        })
        .then((res) => {
          this.tableData = res.data.data.result
          this.total = res.data.data.total
        })
    },
    getParentList() {
      this.$http
        .get('categories', {
          params: {
            type: 2
          }
        })
        .then((res) => {
          //   console.log(res.data.data)
          this.parentCateList = res.data.data
        })
    }
  }
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>
